package lbframework.bean.time;

import javax.swing.UIManager;
import lbframework.bean.panel.LBPanel;
import lbframework.classe.Util;
import lbframework.classe.LBException;

public class LBTime extends LBPanel {

    private boolean obrigatorio = false;

    public LBTime() {
        initComponents();
    }

    public String getClipTextHoraInicio() throws Exception {
        return txtHoraInicio.getClipText();
    }

    public String getClipTextHoraTermino() throws Exception {
        return txtHoraTermino.getClipText();
    }

    public String getHoraInicio() throws Exception {
        return txtHoraInicio.getText();
    }

    public String getHoraTermino() throws Exception {
        return txtHoraTermino.getText();
    }

    private void validarIntervaloHora() throws Exception {
        if (!txtHoraInicio.getClipText().isEmpty() && !txtHoraTermino.getClipText().isEmpty()) {
            if (Integer.parseInt(txtHoraInicio.getClipText()) > Integer.parseInt(txtHoraTermino.getClipText())) {
                txtHoraInicio.requestFocusLater();
                throw new LBException("Intervalo inválido!");
            }
        }
    }

    public boolean isObrigatorio() {
        return obrigatorio;
    }

    public void setObrigatorio(boolean obrigatorio) {
        this.obrigatorio = obrigatorio;

        if (this.isEnabled()) {
            if (obrigatorio) {
                this.setBackground(Util.COR_OBRIGATORIO);
            } else {
                this.setBackground(UIManager.getColor("TextField.background"));
            }
        } else {
            this.setBackground(UIManager.getColor("TextField.disabledBackground"));
        }
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtHoraInicio = new lbframework.bean.textField.LBTextField();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        txtHoraTermino = new lbframework.bean.textField.LBTextField();

        setBorder(null);

        txtHoraInicio.setMascara("Hora");
        txtHoraInicio.setPreferredSize(new java.awt.Dimension(20, 20));
        txtHoraInicio.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtHoraInicioFocusLost(evt);
            }
        });

        vRLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        vRLabel1.setText("a");
        vRLabel1.setPreferredSize(new java.awt.Dimension(20, 10));

        txtHoraTermino.setMascara("Hora");
        txtHoraTermino.setPreferredSize(new java.awt.Dimension(20, 20));
        txtHoraTermino.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtHoraTerminoFocusLost(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(txtHoraInicio, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtHoraTermino, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtHoraInicio, txtHoraTermino});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtHoraInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(txtHoraTermino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtHoraTerminoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtHoraTerminoFocusLost
        try {
            validarIntervaloHora();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_txtHoraTerminoFocusLost

    private void txtHoraInicioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtHoraInicioFocusLost
        try {
            validarIntervaloHora();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, "Atenção");
        }
    }//GEN-LAST:event_txtHoraInicioFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.textField.LBTextField txtHoraInicio;
    private lbframework.bean.textField.LBTextField txtHoraTermino;
    private lbframework.bean.label.LBLabel vRLabel1;
    // End of variables declaration//GEN-END:variables
}
